$(document).ready(function(){
	var VAL = {
			T_FBPBISDAT : "FBPBISDAT",
			T_FAPUASGRP : "FAPUASGRP",
			T_FBPBISTOL : "FBPBISTOL",
			T_FBPBISOPE : "FBPBISOPE",
			NORMAL : "0000000",
            EVT_USR : $("#userId").text(),
			DISABLED_ATTR : {
				"disabled" : true
			},
			ENABLED_ATTR : {
				"disabled" : false
			}
		};
	var domObj = {
			W   : $(window),
			
			$usrListGrd : $("#usrListGrd"),
			$usrListDiv : $("#usrListDiv"),
			usrListPg : "usrListPg",
			$opeListGrd : $("#opeListGrd"),
			$opeListDiv : $("#opeListDiv"),
			opeListPg : "opeListPg",
			$toolListGrd : $("#toolListGrd"),
			$toolListDiv : $("#toolListDiv"),
			toolListPg : "toolListPg",
			$destShopListGrd : $("#destShopListGrd"),
			$destShopListDiv : $("#destShopListDiv"),
			destShopListPg : "destShopListPg",
			
			buttons : {
				$query_btn 		: $("#query_btn"),
				$register_btn   : $("#register_btn")
			}
		};

	var controlsFunc = {
			iniUsrGrd : function(){
				var itemInfoCM = 
					   [{  name  : 'usr_id'    ,  index : 'usr_id',  label :  USER_ID_TAG  ,width : 180 },
						{  name  : 'usr_name'    ,  index : 'usr_name',   label : USER_NAME_TAG,width : 160 }];
				domObj.$usrListGrd.jqGrid({
					datatype : "local",
					autoheight : true,
					mtype : "POST",
					autowidth : true,//宽度根据父元素自适应
					shrinkToFit : true,
					scroll : true,
					resizable : true,
					rownumbers : true,
					loadonce : true,
					viewrecords : true,
					colModel : itemInfoCM,
					pager : domObj.usrListPg,
					onSelectRow : function(id) {
						controlsFunc.onSelUserGrdFnc(id);
					}
				});
			},
			iniOpeGrd : function(){
				var itemInfoCM = 
					   [{  name  : 'ope_id'    ,  index : 'ope_id',  label :  OPE_ID_TAG  ,width : 210 },
						{  name  : 'ope_dsc'    ,  index : 'ope_dsc',   label : OPE_DSC_TAG,width : 210 }];
				domObj.$opeListGrd.jqGrid({
					datatype : "local",
					autoheight : true,
					mtype : "POST",
					height : 420,
					width : 456,
					autowidth : true,//宽度根据父元素自适应
					shrinkToFit : true,
					scroll : true,
					resizable : true,
					rownumbers : true,
					loadonce : true,
					viewrecords : true,
					multiselect : true,
					colModel : itemInfoCM,
					pager : domObj.opeListPg,
					onSelectRow : function(id) {
					}
				});
			},
			iniToolGrd : function(){
				var itemInfoCM = 
					   [{  name  : 'tool_id'    ,  index : 'tool_id',  label :  TOOL_ID_TAG  ,width : 210 },
						{  name  : 'tool_dsc'    ,  index : 'tool_dsc',   label : TOOL_DSC_TAG,width : 210 }];
				domObj.$toolListGrd.jqGrid({
					datatype : "local",
					autoheight : true,
					mtype : "POST",
					height : 420,
					width : 456,
					autowidth : true,//宽度根据父元素自适应
					shrinkToFit : true,
					scroll : true,
					resizable : true,
					rownumbers : true,
					loadonce : true,
					viewrecords : true,
					multiselect : true,
					colModel : itemInfoCM,
					pager : domObj.toolListPg,
					onSelectRow : function(id) {
					}
				});
			},
			iniDestShopGrd : function(){
				var itemInfoCM = 
					   [{  name  : 'data_ext'    ,  index : 'data_ext',  label :  DEST_SHOP_TAG  ,width : 210 },
						{  name  : 'data_desc'    ,  index : 'data_desc',   label : DEST_DESC_TAG,width : 210 }];
				domObj.$destShopListGrd.jqGrid({
					datatype : "local",
					autoheight : true,
					mtype : "POST",
					height : 420,
					width : 456,
					autowidth : true,//宽度根据父元素自适应
					shrinkToFit : true,
					scroll : true,
					resizable : true,
					rownumbers : true,
					loadonce : true,
					viewrecords : true,
					multiselect : true,
					colModel : itemInfoCM,
					pager : domObj.destShopListPg,
					onSelectRow : function(id) {
					}
				});
			},
			queryFunc:function(){
				$("#usrSelectDialog").modal({
						backdrop:true,
						keyboard:false,
						show:false
				});
				$("#usrSelectDialog").unbind('shown.bs.modal');
				$("#usrSelectDialog_sureBtn").unbind('click');
				$("#usrSelectDialog").bind('shown.bs.modal');
				$("#usrSelectDialog").modal("show");
				
				$("#usrSelectDialog_queryBtn").bind('click',dialogUsrFnc);
				$("#usrSelectDialog_idTxt").attr({'disabled':false});
		    	$("#usrSelectDialog_idTxt").val("");
			},
			onSelUserGrdFnc: function(id){
				var rowData = domObj.$usrListGrd.jqGrid('getRowData',id);
				var inOpeObj = {
						trx_id : VAL.T_FBPBISOPE,
						action_flg : 'L'
				};
				var outOpeObj = comTrxSubSendPostJson(inOpeObj);
				if (outOpeObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outOpeObj.oary,"#opeListGrd");
				}
				var inToolObj = {
						trx_id : VAL.T_FBPBISTOL,
						action_flg : 'L'
				};
				var outToolObj = comTrxSubSendPostJson(inToolObj);
				if (outToolObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outToolObj.oary,"#toolListGrd");
				}
				var inDestObj = {
						trx_id : VAL.T_FBPBISDAT,
						action_flg : 'Q',
						iary : [{
							data_cate: 'DEST'
						}]
				};
				var outDestObj = comTrxSubSendPostJson(inDestObj);
				if (outDestObj.rtn_code === VAL.NORMAL) {
					setGridInfo(outDestObj.oary,"#destShopListGrd");
				}
				var inTrxObj = {
						trx_id : VAL.T_FAPUASGRP,
						action_flg: 'H',
						iaryB : [{
							usr_id_fk : rowData.usr_id
						}]
				};
				var outTrxObj = comTrxSubSendPostJson(inTrxObj);
				if (outTrxObj.rtn_code === VAL.NORMAL) {
					var opeRowIds = domObj.$opeListGrd.jqGrid('getDataIDs')
					var opeRows = domObj.$opeListGrd.jqGrid('getRowData');
					var toolRowIds = domObj.$toolListGrd.jqGrid('getDataIDs')
					var toolRows = domObj.$toolListGrd.jqGrid('getRowData');
					var destRowIds = domObj.$destShopListGrd.jqGrid('getDataIDs')
					var destRows = domObj.$destShopListGrd.jqGrid('getRowData');
					
					var oaryB = outTrxObj.oaryB;
					var prop_ids = [];
					for (var j = 0; j < oaryB.length; j++) {
						var  prop_id = oaryB[j].prop_id;
						prop_ids.push(prop_id);
					};
					for (var i = 0; i < opeRows.length; i++) {
						if ($.inArray(opeRows[i].ope_id,prop_ids) != -1) {
							 domObj.$opeListGrd.setSelection(opeRowIds[i],false);
						}
					};
					for (var i = 0; i < toolRows.length; i++) {
						if ($.inArray(toolRows[i].tool_id,prop_ids) != -1) {
							 domObj.$toolListGrd.setSelection(toolRowIds[i],false);
						}
					};
					for (var i = 0; i < destRows.length; i++) {
						if ($.inArray(destRows[i].data_id,prop_ids) != -1) {
							 domObj.$destShopListGrd.setSelection(destRowIds[i],false);
						}
					};
				}
			},
			registerFunc : function (){
				var opeGrdRowData,toolGrdRowData,destShopGrdRowData,ope_id,tool_id,data_ext,iary=[];
				var usrGrdId = domObj.$usrListGrd.jqGrid('getGridParam','selrow');
				if (!usrGrdId) {
					showErrorDialog('','请选择用户');
					return;
				}
				var usrGrdRowData = domObj.$usrListGrd.jqGrid('getRowData',usrGrdId);
				var usr_id_fk = usrGrdRowData.usr_id;
				var opeGrdIds = domObj.$opeListGrd.jqGrid('getGridParam','selarrrow');
				var toolGrdIds = domObj.$toolListGrd.jqGrid('getGridParam','selarrrow');
				var destShopGrdIds = domObj.$destShopListGrd.jqGrid('getGridParam','selarrrow');
				
				for (var i = 0; i < opeGrdIds.length; i++) {
					opeGrdRowData = domObj.$opeListGrd.jqGrid('getRowData',opeGrdIds[i]);
					ope_id = opeGrdRowData.ope_id;
					var iaryB = {
							prop_id : ope_id,
							prop_cate  : 'OPE',
							usr_id_fk  : usr_id_fk
					};
					iary.push(iaryB);
				}
				for (var i = 0; i < toolGrdIds.length; i++) {
					toolGrdRowData = domObj.$toolListGrd.jqGrid('getRowData',toolGrdIds[i]);
					tool_id = toolGrdRowData.tool_id;
					var iaryB = {
							prop_id : tool_id,
							prop_cate  : 'TOOL',
							usr_id_fk  : usr_id_fk
					};
					iary.push(iaryB);
				}
				for (var i = 0; i < destShopGrdIds.length; i++) {
					destShopGrdRowData = domObj.$destShopListGrd.jqGrid('getRowData',destShopGrdIds[i]);
					data_ext = destShopGrdRowData.data_ext;
					var iaryB = {
							prop_id : data_ext,
							prop_cate  : 'DEST',
							usr_id_fk  : usr_id_fk
					};
					iary.push(iaryB);
				}
				var inObj = {
						trx_id : VAL.T_FAPUASGRP,
						action_flg : 'I',
						evt_usr : VAL.EVT_USR,
						iaryB : iary
				};
				var outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code === VAL.NORMAL) {
					showSuccessDialog("登记成功!");
				}
			}
	};
	
	function dialogUsrFnc(usr_id){
		var iary = {};
		var usr_id =$("#usrSelectDialog_idTxt").val();
    	if(usr_id !=""){
    		iary.usr_id  = usr_id ;
    	}
    	
    	 var inTrxObj ={
    			 trx_id     : VAL.T_FAPUASGRP ,
    			 action_flg : "Q"        ,
    			 iary       : [iary]
    	 }
    	 
    	 var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
    	 if(  outTrxObj.rtn_code == _NORMAL ) {
    		 setGridInfo(outTrxObj.oary,"#usrListGrd");
    		 $('#usrSelectDialog').modal("hide");
    	 }
    }
	
	function iniClearFunc(){
		domObj.$usrListGrd.jqGrid("clearGridData");
	}
	
	function iniButtonAction(){
		domObj.buttons.$query_btn.click(function(){
			controlsFunc.queryFunc();
		});
		domObj.buttons.$register_btn.click(function(){
			controlsFunc.registerFunc();
		});
		$("#usrSelectDialog_idTxt").keydown(function(event){
			if (event.keyCode == 13 ) {
	        	var usr_id =$("#usrSelectDialog_idTxt").val();
	        	dialogUsrFnc(usr_id);
			}
	    });
	}
	
	 var otherActionBind = function(){
	        //Stop from auto commit
	        $("form").submit(function(){
	            return false;
	        });
	       
	    };
	
	function initFunc(){
		iniClearFunc();
		controlsFunc.iniUsrGrd();
		controlsFunc.iniOpeGrd();
		controlsFunc.iniToolGrd();
		controlsFunc.iniDestShopGrd();
		iniButtonAction();
		otherActionBind();
	}
	initFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth,offsetBottom1, divWidth1,offsetBottom2, divWidth2,offsetBottom3, divWidth3;
        divWidth = $("#usrListDiv").width();
        offsetBottom =  $(window).height() - $("#usrListDiv").offset().top;
        $("#usrListDiv").height(offsetBottom * 0.95);
        $("#usrListGrd").setGridWidth(divWidth * 0.95);
        $("#usrListGrd").setGridHeight(offsetBottom * 0.99 - 101);
        //2
        divWidth1 = $("#divTotal").width();
        offsetBottom1 =  $(window).height() - $("#usrListDiv").offset().top;
        $("#divTotal").height(offsetBottom1 * 0.95);
        $("#opeListGrd").setGridWidth(divWidth1 * 0.95);
        $("#opeListGrd").setGridHeight(offsetBottom1 * 0.99 - 101);
        //3
        divWidth2 = $("#divTotal").width();
        offsetBottom2 =  $(window).height() - $("#usrListDiv").offset().top;
        $("#divTotal").height(offsetBottom2 * 0.95);
        $("#toolListGrd").setGridWidth(divWidth2 * 0.95);
        $("#toolListGrd").setGridHeight(offsetBottom2 * 0.99 - 101);
        //4
        divWidth3 = $("#divTotal").width();
        offsetBottom3 =  $(window).height() - $("#usrListDiv").offset().top;
        $("#divTotal").height(offsetBottom3 * 0.95);
        $("#destShopListGrd").setGridWidth(divWidth3 * 0.95);
        $("#destShopListGrd").setGridHeight(offsetBottom3 * 0.99 - 101);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});